package com.smarthospital.common.util;

import com.smarthospital.common.Result.CodeMsg;
import com.smarthospital.common.Result.CommonException;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.util.Date;

/**
 * create by sym  2021/5/4 11:05.
 */
public class SHDateUtil {
    public static String MM_DD = "MM-dd";
    public static String M_DD = "M-dd";
    public static String YYYY_MM_DD = "yyyy-MM-dd";
    public static String yyyMMddHHmmss = "yyyy-MM-dd HH:mm:ss";
    public static String yyyMMddHHmm = "yyyy-MM-dd HH:mm";

    /**
     * 时间格式  2021-05-03
     * @param date
     * @return
     */
    public static boolean isAfterToday(String date) {
        SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD);
        String todayStr = sdf.format(new Date());
        String dateS = date.replaceAll("-", "").replaceAll(" ", "");
        String todayS = todayStr.replaceAll("-", "").replaceAll(" ", "");
        return Integer.valueOf(dateS).compareTo(Integer.valueOf(todayS))>=0;
    }

    public static void main(String[] args) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(yyyMMddHHmm);

        System.out.println(sdf.parse(null) );
    }

    /**
     * 3-05 3-06
     */
    public static String dateStr(int i) {
        LocalDateTime dateTime = LocalDateTime.now().plusDays(i);
        Instant instant = dateTime.toInstant(ZoneOffset.UTC);
        Date date = Date.from(instant);
        return getDateStr(date,M_DD);
    }

    /**
     * now_yyyMMddHHmmss
     */
    public static String now_yyyMMddHHmmss() {
       SimpleDateFormat sdf = new SimpleDateFormat(yyyMMddHHmmss);
        return sdf.format(new Date());
    }

    /**
     * 2021-03-05
     */
    public static String dbDateStr(int i) {
        LocalDateTime dateTime = LocalDateTime.now().plusDays(i);
        Instant instant = dateTime.toInstant(ZoneOffset.UTC);
        Date date = Date.from(instant);
        return getDateStr(date,YYYY_MM_DD);
    }


    public static String getDateStr(Date date,String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

    public static String weekNumStr(int i) {
        LocalDateTime dateTime = LocalDateTime.now().plusDays(i);
        DayOfWeek dayOfWeek = dateTime.getDayOfWeek();
        switch (dayOfWeek){
            case MONDAY:
                return "星期一";
            case TUESDAY:
                return "星期二";
            case WEDNESDAY:
                return "星期三";
            case THURSDAY:
                return "星期四";
            case FRIDAY:
                return "星期五";
            case SATURDAY:
                return "星期六";
            case SUNDAY:
                return "星期日";
            default:
                return "unkown";
        }

    }

    /**
     * 08:00-08:30  11:30-12:00
     * 获取排班时间 格式
     * 如果23点
     * @return
     */
    public static String getRangeTime() {
        LocalDateTime now  = LocalDateTime.now();
        SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD);
        String format = sdf.format(new Date());
        int hour = now.getHour();
        int minute = now.getMinute();
        if (minute >= 30) {
            return format+ " " + SHStringUtil.complString(hour+"",2,"0") + ":30" + "-"
                    +SHStringUtil.complString((hour+1)+"",2,"0")+ ":00";
        }else {
            return format+ " " + SHStringUtil.complString(hour+"",2,"0") + ":00" + "-"
                    +SHStringUtil.complString(hour+"",2,"0")+ ":30";
        }

    }

    /**
     * 比较两个  2021-05-21 08:30   2021-05-21 09:30
     * @param rangeTime1
     * @param rangeTime2
     * @return
     */
    public static int compareRangeTime(String rangeTime1,String rangeTime2) {
        String s1 = rangeTime1.replaceAll("-", "").replaceAll(":", "").replaceAll(" ", "");
        String s2 = rangeTime2.replaceAll("-", "").replaceAll(":", "").replaceAll(" ", "");
        long l = Long.valueOf(s1) - Long.valueOf(s2);
        return Integer.valueOf(l+"");
    }

    /**
     * 2021-05-21 08:30
     * 根据开始时间获得结束时间
     * @param inquiryStartTime 开始时间
     * @return
     */
    public static String getRangeEndTime(String inquiryStartTime) throws ParseException {
        String[] split = inquiryStartTime.split(" ");
        if (split.length != 2) {
            throw new CommonException(CodeMsg.ERROR_RANGE_TIME_PARTERN);
        }
        SimpleDateFormat sdf = new SimpleDateFormat(yyyMMddHHmm);
        Date date = sdf.parse(inquiryStartTime);
        LocalDateTime localDateTime = LocalDateTime.ofInstant (date.toInstant(),ZoneId.systemDefault());
        //加30分钟
        localDateTime = localDateTime.plusMinutes(30);
        Instant instant = localDateTime.toInstant(ZoneOffset.ofHours(8));

        return sdf.format(Date.from(instant)) ;
    }

    /**
     * 当前时间是否超过结束时间
     * 2021-05-21 08:30
     * @param rangeEndTime
     * @return
     */
    public static boolean isAfterRangeTime(String rangeEndTime) {
        SimpleDateFormat sdf = new SimpleDateFormat(yyyMMddHHmm);
        String now = sdf.format(new Date());
        return compareRangeTime( rangeEndTime,now ) > 0;
    }

    /**
     * 检验排班时间格式
     * @param inquiryTime
     */
    public static void checkRangeTimePattern(String inquiryTime) {
        SimpleDateFormat sdf = new SimpleDateFormat(yyyMMddHHmm);
        try {
            sdf.parse(inquiryTime);
        } catch (Exception e) {
            throw new CommonException(CodeMsg.ERROR_RANGE_TIME_PARTERN);
        }
    }


}
